relocate no output format display routine. (#486)
authortsteven4 <13596209+tsteven4@users.noreply.github.com>
Tue, 4 Feb 2020 16:31:45 +0000 (09:31 -0700)
committerGitHub <noreply@github.com>
Tue, 4 Feb 2020 16:31:45 +0000 (09:31 -0700)
* relocate no output format display routine.

waypt_disp() is used by main when no output format is specified.
This moves that routine from waypt.cc to main.cc.

* clean up exit from main.

don't leave the global_waypoint_list lying around.

return from main instead of calling exit.

defs.h
main.cc
waypt.cc

diff --git a/defs.h b/defs.h
index ea8a6413a3e064aad00db95da77053358fe5f5bc..b1e456f8e8f43ad89f5b37aaf200bdeb7fbb4b23 100644 (file)
--- a/defs.h
+++ b/defs.h
@@ -608,7 +608,6 @@ void waypt_init();
 void waypt_add(Waypoint* wpt);
 void waypt_del(Waypoint* wpt);
 unsigned int waypt_count();
-void waypt_disp(const Waypoint* wpt);
 void waypt_status_disp(int total_ct, int myct);
 //void waypt_disp_all(waypt_cb); /* template */
 //void waypt_disp_session(const session_t* se, waypt_cb cb); /* template */
@@ -618,6 +617,7 @@ void waypt_add_to_bounds(bounds* bounds, const Waypoint* waypointp);
 void waypt_compute_bounds(bounds* bounds);
 Waypoint* find_waypt_by_name(const QString& name);
 void waypt_flush_all();
+void waypt_deinit();
 void waypt_append(WaypointList* src);
 void waypt_backup(WaypointList** head_bak);
 void waypt_restore(WaypointList* head_bak);
diff --git a/main.cc b/main.cc
index dba7917f0255f125b02c999922a85460c5d9ecb6..bc7f56097ee34e2f3445f105e2be45fcd2241b13 100644 (file)
--- a/main.cc
+++ b/main.cc
@@ -20,7 +20,6 @@
 #include <clocale>                  // for setlocale, LC_NUMERIC, LC_TIME
 #include <csignal>                  // for signal, SIGINT, SIG_ERR
 #include <cstdio>                   // for printf, fgetc, stdin
-#include <cstdlib>                  // for exit
 #include <cstring>                  // for strcmp
 
 #include <QtCore/QByteArray>        // for QByteArray
@@ -198,6 +197,43 @@ signal_handler(int sig)
   tracking_status.request_terminate = 1;
 }
 
+class FallbackOutput
+{
+public:
+  FallbackOutput() : mkshort_handle(mkshort_new_handle()) {}
+  // delete copy and move constructors and assignment operators.
+  // The defaults are not appropriate, and we haven't implemented proper ones.
+  FallbackOutput(const FallbackOutput&) = delete;
+  FallbackOutput& operator=(const FallbackOutput&) = delete;
+  FallbackOutput(FallbackOutput&&) = delete;
+  FallbackOutput& operator=(FallbackOutput&&) = delete;
+  ~FallbackOutput() {mkshort_del_handle(&mkshort_handle);}
+
+  void waypt_disp(const Waypoint* wpt)
+  {
+    if (wpt->GetCreationTime().isValid()) {
+      printf("%s ", qPrintable(wpt->creation_time.toString()));
+    }
+    printposn(wpt->latitude,1);
+    printposn(wpt->longitude,0);
+    if (!wpt->description.isEmpty()) {
+      printf("%s/%s",
+             global_opts.synthesize_shortnames ?
+             qPrintable(mkshort(mkshort_handle, wpt->description)) :
+             qPrintable(wpt->shortname),
+             qPrintable(wpt->description));
+    }
+  
+    if (wpt->altitude != unknown_alt) {
+      printf(" %f", wpt->altitude);
+    }
+    printf("\n");
+  }
+
+private:
+  short_handle mkshort_handle;
+};
+
 static int
 run(const char* prog_name)
 {
@@ -210,6 +246,8 @@ run(const char* prog_name)
   int opt_version = 0;
   bool did_something = false;
   QStack<QargStackElement> qargs_stack;
+  FallbackOutput fbOutput;
+
 
   // Use QCoreApplication::arguments() to process the command line.
   QStringList qargs = QCoreApplication::arguments();
@@ -518,7 +556,10 @@ run(const char* prog_name)
   }
   if (ovecs == nullptr) {
     cet_convert_init(CET_CHARSET_ASCII, 1);
-    waypt_disp_all(waypt_disp);
+    auto waypt_disp_lambda = [&fbOutput](const Waypoint* wpt)->void {
+      fbOutput.waypt_disp(wpt);
+    };
+    waypt_disp_all(waypt_disp_lambda);
   }
 
   /*
@@ -567,7 +608,7 @@ run(const char* prog_name)
 //          ovecs->wr_position_deinit();
         } else {
           /* Just print to screen */
-          waypt_disp(wpt);
+          fbOutput.waypt_disp(wpt);
         }
         delete wpt;
       }
@@ -665,12 +706,12 @@ main(int argc, char* argv[])
 
   rc = run(prog_name);
 
-  waypt_flush_all();
   route_deinit();
+  waypt_deinit();
   session_exit();
-  Vecs::Instance().exit_vecs();
   FilterVecs::Instance().exit_filter_vecs();
+  Vecs::Instance().exit_vecs();
   inifile_done(global_opts.inifile);
 
-  exit(rc);
+  return rc;
 }
index 67921e8ac545cc19d62d3dda2530a8050c33cba0..db9375385bb64b2fadf4465b12a92d03dfad0bb4 100644 (file)
--- a/waypt.cc
+++ b/waypt.cc
 #include <cassert>              // for assert
 #include <cmath>                // for fabs
 #include <cstdio>               // for printf, fflush, fprintf, stdout
-#include <ctime>                // for time_t
 #include <algorithm>            // for stable_sort
 
-#include <QtCore/QByteArray>    // for QByteArray
 #include <QtCore/QChar>         // for QChar
 #include <QtCore/QDateTime>     // for QDateTime
 #include <QtCore/QList>         // for QList
@@ -42,7 +40,6 @@
 
 WaypointList* global_waypoint_list;
 
-static short_handle mkshort_handle;
 geocache_data Waypoint::empty_gc_data;
 static global_trait traits;
 
@@ -54,7 +51,6 @@ const global_trait* get_traits()
 void
 waypt_init()
 {
-  mkshort_handle = mkshort_new_handle();
   global_waypoint_list = new WaypointList;
 }
 
@@ -92,29 +88,6 @@ waypt_count()
   return global_waypoint_list->count();
 }
 
-// TODO: should this, and mkshort_handle, be part of main, which is the only user?
-void
-waypt_disp(const Waypoint* wpt)
-{
-  if (wpt->GetCreationTime().isValid()) {
-    printf("%s ", qPrintable(wpt->creation_time.toString()));
-  }
-  printposn(wpt->latitude,1);
-  printposn(wpt->longitude,0);
-  if (!wpt->description.isEmpty()) {
-    printf("%s/%s",
-           global_opts.synthesize_shortnames ?
-           qPrintable(mkshort(mkshort_handle, wpt->description)) :
-           qPrintable(wpt->shortname),
-           qPrintable(wpt->description));
-  }
-
-  if (wpt->altitude != unknown_alt) {
-    printf(" %f", wpt->altitude);
-  }
-  printf("\n");
-}
-
 void
 waypt_status_disp(int total_ct, int myct)
 {
@@ -190,12 +163,16 @@ find_waypt_by_name(const QString& name)
 void
 waypt_flush_all()
 {
-  if (mkshort_handle) {
-    mkshort_del_handle(&mkshort_handle);
-  }
   global_waypoint_list->flush();
 }
 
+void
+waypt_deinit()
+{
+  waypt_flush_all();
+  delete global_waypoint_list;
+}
+
 void
 waypt_append(WaypointList* src)
 {